package com.adcc.monitoragent.mina;

import java.nio.ByteBuffer;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adcc.monitoragent.util.GzipUtil;

 
/**
 * <b>function:</b> 字符编码、解码工厂类，编码过滤工厂
 * @author hoojo
 * @createDate 2012-6-26 下午01:08:50
 * @file CharsetCodecFactory.java
 * @package com.hoo.mina.code.factory
 * @project ApacheMiNa
 * @blog http://blog.csdn.net/IBM_hoojo
 * @email hoojo_@126.com
 * @version 1.0
 */
public class CharsetCodecFactoryForUDP implements ProtocolCodecFactory {
	private CharsetDecoder d = new CharsetDecoder();
	private CharsetEncoder e = new CharsetEncoder();
	
    public ProtocolDecoder getDecoder(IoSession session) throws Exception {
        return d;
    }
 
    public ProtocolEncoder getEncoder(IoSession session) throws Exception {
        return e;
    }
    /**
     * 字符解码器
     * @author charlie
     *
     */
    public static class CharsetDecoder implements ProtocolDecoder {
    	   private final static Logger log = LoggerFactory.getLogger(CharsetDecoder.class);
    	    
    	    @Override
    	    public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
    	        log.debug("#########decode#########");
    	        
    	        // 如果有消息
    	        while (in.hasRemaining()) {
    	        	ByteBuffer bb = in.buf();
    	        	byte[] buffer = new byte[bb.limit()];
    	        	bb.get(buffer);
    	        	String msg = GzipUtil.bytesToObjGzip(buffer).toString();
    	        	if(msg == null) {
    	        		out.write(new String(buffer, 0, buffer.length));
    	        	} else {
    	        		out.write(msg);
    	        	}
    	        
    	        }
    	    }
    	 
    	    @Override
    	    public void dispose(IoSession session) throws Exception {
    	        log.debug("#########dispose#########");
    	        log.debug(session.getCurrentWriteMessage().toString());
    	    }
    	 
    	    @Override
    	    public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws Exception {
    	        log.debug("#########完成解码#########");
    	    }
    	}
    
    /**
     * <b>function:</b> 字符编码器
     * @author charlie
     */
    public static class CharsetEncoder implements ProtocolEncoder {
        private final static Logger log = LoggerFactory.getLogger(CharsetEncoder.class);
        
        @Override
        public void dispose(IoSession session) throws Exception {
            log.debug("#############dispose############");
        }
     
        @Override
        public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
            log.debug("#############字符编码############");
            byte[] strMessage = message.toString().getBytes();
            IoBuffer buff = IoBuffer.allocate(strMessage.length);
            buff.setAutoExpand(true);
            buff.put(strMessage);
            buff.flip();
            out.write(buff);
        }
    }
}